package newcoder_exp.justPractise;
import java.util.*;
//最长无重复子数组，O(N)解法

import org.junit.Test;
public class Nc41 {

    public int maxLength (int[] arr) {
        int len;
        if (arr == null || (len = arr.length) == 0) return 0;
        //记录窗口中的下标
        Deque<Integer> deque = new LinkedList<>();
        //记录元素以及在窗口中出现的次数[设置为出现就行]
        Map<Integer, Integer> map = new HashMap<>();
        int res = 1;
        for (int i = 0; i < len; i++) {
            while (map.containsKey(arr[i])) {
                int p = deque.pollFirst();
                int t = map.get(arr[p]) - 1;
                if (t == 0) {
                    map.remove(arr[p]);
                } else {
                    map.put(arr[p], t);
                }
            }
            deque.offerLast(i);
            map.put(arr[i], 1);
            res = Math.max(res, deque.size());
        }
        return res;
    }

    @Test
    public void test() {
        int[] nums = {1 , 2, 3, 2};
        int res= maxLength(nums);
        System.out.println(res);
    }
}
